#include "stm32f4xx.h"                  // Device header
#include <string.h>
#include <stdio.h>

typedef struct
{
	uint32_t rcuGpio;
	uint32_t rcuUart;
	USART_TypeDef* uartNo;
	GPIO_TypeDef* gpio;
	uint8_t  gpioAf;
	uint16_t txPin;
	uint16_t txPinAf;
	uint16_t rxPin;
	uint16_t rxPinAf;		
} WifiHwInfo_t;

static WifiHwInfo_t g_wifiHwInfo =
{
	RCC_AHB1Periph_GPIOA, 
	RCC_APB1Periph_USART2, 
	USART2, 
	GPIOA, 
	GPIO_AF_USART2,
	GPIO_Pin_5,
	GPIO_PinSource5,
	GPIO_Pin_6,
	GPIO_PinSource6
};

static void WifiGpioConfig(void)
{
	RCC_AHB1PeriphClockCmd(g_wifiHwInfo.rcuGpio, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Pin = g_wifiHwInfo.rxPin | g_wifiHwInfo.txPin;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(g_wifiHwInfo.gpio, &GPIO_InitStructure);
	
	GPIO_PinAFConfig(g_wifiHwInfo.gpio, g_wifiHwInfo.rxPinAf, g_wifiHwInfo.gpioAf);
	GPIO_PinAFConfig(g_wifiHwInfo.gpio, g_wifiHwInfo.txPinAf, g_wifiHwInfo.gpioAf);
}

static void WifiNvicConfig(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_Init(&NVIC_InitStructure);
}

static void WifiUsartConfig(uint32_t baudRate)
{
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = baudRate;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(g_wifiHwInfo.uartNo, &USART_InitStructure);
	
	USART_ITConfig(g_wifiHwInfo.uartNo, USART_IT_RXNE, ENABLE);
	USART_ITConfig(g_wifiHwInfo.uartNo, USART_IT_IDLE, ENABLE);
	
	USART_Cmd(g_wifiHwInfo.uartNo, ENABLE);
}

void Wifi_Init(uint32_t baudRate)
{
	WifiGpioConfig();
	WifiNvicConfig();
	WifiUsartConfig(baudRate);
}

//uint8_t wifi_rec_flag = 0;
////uint8_t wifi_work_flag = 0;
//uint8_t wifi_buf[1024] = {0};
//uint32_t wifi_index = 0;


#define WIFI_RECV_MAX_NUM 256

uint8_t wifi_rxd_buf[WIFI_RECV_MAX_NUM] = {0};
uint32_t wifi_rxd_index = 0;
uint8_t wifi_rxd_flag = 0;

void USART2_RecTask(void)
{
	if (wifi_rxd_flag)
	{
		for (uint32_t i = 0; i < wifi_rxd_index; i++)
		{
			printf("%c", wifi_rxd_buf[i]);
		}
	
		wifi_rxd_flag = 0;
		wifi_rxd_index = 0;
		memset(wifi_rxd_buf, 0, WIFI_RECV_MAX_NUM);
	}
}

void USART2_IRQHandler(void)
{
	uint16_t idleClear;
	if (USART_GetITStatus(g_wifiHwInfo.uartNo, USART_IT_RXNE) != RESET)
	{
		USART_ClearITPendingBit(g_wifiHwInfo.uartNo, USART_IT_RXNE);
		wifi_rxd_buf[wifi_rxd_index++] = USART_ReceiveData(g_wifiHwInfo.uartNo); 
	}
	else if (USART_GetITStatus(g_wifiHwInfo.uartNo, USART_IT_IDLE) != RESET)
	{
		wifi_rxd_flag = 1;
		USART2_RecTask();
		idleClear = g_wifiHwInfo.uartNo->SR;
		idleClear = g_wifiHwInfo.uartNo->DR;	
	}
}

